using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration; 
using NUnit.Framework;
using HermesPortal.Core; 

namespace HermesPortal.NUnit
{

    [TestFixture] 
    public class RealmServicesFixture
    {

        private int _sagaID= 0;
        private int _realmID = 0;
        private int _creatorID =0;
        private int _testStoryGuide = 0;
        private int _testPlayer = 0;
        private List<Realm> realms = null; 
        [TestFixtureSetUp] 
        public void FixtureSetUp()
        {   _creatorID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestCreator"]);
            _testStoryGuide = Convert.ToInt32(ConfigurationSettings.AppSettings["TestStoryGuide"]);
            _testPlayer = Convert.ToInt32(ConfigurationSettings.AppSettings["TestPlayer"]);
            _sagaID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestSaga"]);
            _realmID = Convert.ToInt32(ConfigurationSettings.AppSettings["TestRealm"]);

            realms = RealmServices.AdminGetRealms(_sagaID);
        }

        [Test]
        public void GetRealmsAdmin()
        {
            List<Realm> realms = RealmServices.AdminGetRealms(_sagaID);
            Assert.IsNotNull(realms, string.Format("Realms List should not be Null for SagaID: {0}", _sagaID));
            foreach (Realm realm in realms)
            {
                ViewBusinessObjectProperties(realm);
            }
        }
        [Test]
        public void GetRealmStoryGuide()
        {
            int realmCount = 0;
            foreach (Realm realm in realms)
            {
                if (realm.IsPrivate && realm.CreatorID != _testStoryGuide)
                    continue;
                else
                    realmCount++;
            }
            List<Realm> viewableRealms = RealmServices.GetRealms(_sagaID, _testStoryGuide );
            //foreach (Realm realm in realms)
            //{
            //    ViewBusinessObjectProperties(realm);
            //}
            Assert.AreEqual(realmCount, viewableRealms.Count);
            Console.WriteLine("RealmCount =" + realmCount.ToString() );   
        }
        [Test]
        public void GetRealmsPlayer()
        {
            int realmCount = 0;
            foreach (Realm realm in realms)
            {
                if (! realm.IsPrivate)
                    realmCount++;
            }
            List<Realm> viewableRealms = RealmServices.GetRealms(_sagaID, _testPlayer);
            foreach (Realm realm in realms)
            {
                ViewBusinessObjectProperties(realm);
            }
            Assert.AreEqual(realmCount, viewableRealms.Count);
        

        }

        [Test]
        public void GetRealm()
        {
            Realm realm = null;
           realm =  RealmServices.GetRealm(_realmID);
            Assert.IsNotNull(realm, string.Format("Realm should not be Null for RealmID: {0}", _realmID ));
            ViewBusinessObjectProperties(realm);
        }




        private void ViewBusinessObjectProperties(object obj)
        {
            Type objType = obj.GetType();
            PropertyInfo[] props = objType.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                FieldInfo fi = objType.GetField(prop.Name);
                Console.WriteLine(prop.Name + ": " + prop.GetValue(obj, null));
            }
        }
    }


}
